<template>
  <view>
    <form @submit="add_submitFn">
      <view class="addbox">
        <textarea :autoFocus="true" maxlength="141" name="content" placeholder="请输入内容，比如您这一刻的想法......"
                  @input="contentFn"></textarea>
        <view class="h4">
          <text class="span">{{ ic }}</text>
          /140
        </view>
        <view class="ul">
          <view class="li" @tap="upFn">
            <view class="i ico"></view>
            <text class="span">上传图片</text>
          </view>
          <view v-for="(item, item_index) in ARR" :key="item_index" class="li">
            <image :src="item" mode="aspectFill"></image>

            <text :data-url="item" class="b ico" @tap.stop.prevent="delFn"></text>
          </view>
        </view>
      </view>
      <button :style="'width:90%;--color:' + color + ';--rgbcolor:' + rgbcolor" class="btn5" formType="submit"
              type="button">保存
      </button>
    </form>
  </view>
</template>

<script>
var t = require('../../res/www.a9a.cn.js');
var app = getApp();
var e = uni.getStorageSync('COOK_U');
export default {
  data() {
    return {
      ic: 0,
      ARR: [],
      color: '',
      rgbcolor: '',
      t: ''
    };
  },
  onLoad: function (e) {
    var that = this;
    app.globalData.get_config('_ZEAI').then(function (a) {
      that.setData({
        color: a.color,
        rgbcolor: t.c16_rgb(a.color),
        t: e.t
      });
      app.globalData.chklogin().then(function (t) {
        t ||
        uni.showModal({
          content: rs.msg,
          confirmColor: that.color,
          confirmText: '朕知道了',
          showCancel: false,
          success: function (t) {
            uni.navigateBack();
          }
        });
      });
    });
  },
  methods: {
    contentFn: function (t) {
      var n = t.detail.value.length;
      if (n > 140) {
        uni.showToast({
          title: '最多140字',
          icon: 'none',
          mask: true
        });
        n = 140;
      }
      this.setData({
        ic: n
      });
    },

    upFn: function () {
      var that = this;
      var n = 6 - parseInt(this.ARR.length);
      if (n <= 0) {
        uni.showToast({
          title: '最多上传6张图片哦',
          icon: 'none',
          mask: true
        });
      } else {
        uni.chooseMedia({
          count: n,
          mediaType: ['image'],
          sizeType: ['compressed'],
          sourceType: ['album', 'camera'],
          success: function (n) {
            for (var e = that.ARR, o = [], a = 0; a < n.tempFiles.length; a++) {
              o.push(n.tempFiles[a].tempFilePath);
            }
            e = e.concat(o);
            that.setData({
              ARR: e
            });
          }
        });
      }
    },

    delFn: function (n) {
      var e = n.currentTarget.dataset.url;
      var o = t.json_remove(this.ARR, e);
      this.setData({
        ARR: o
      });
    },

    add_submitFn: function (o) {
      var that = this;
      var s = o.detail.value.content;
      if (t.empty(s) || s.length > 140) {
        uni.showToast({
          title: '亲，该说点什么吧～',
          icon: 'none',
          mask: true
        });
      } else {
        uni.showModal({
          title: '温馨提醒',
          content: '确定提交么？\n以下情形话题将被删除或封号：\n1．请勿留下任何形式联系方法\n2．请勿发表涉黄涉政相关言论\n3．请勿发表辱骂攻击他人言论',
          confirmColor: that.color,
          cancelText: '取消',
          confirmText: '确定',
          success: function (o) {
            if (o.confirm) {
              var i = that.ARR;
              var c = i.length;
              if (c > 0) {
                var r = Array();
                var l = 0;
                !(function o() {
                  uni.showLoading({
                    title: '正在上传..第' + parseInt(l + 1),
                    mask: true
                  });
                  uni.uploadFile({
                    url: app.globalData.WWW + '/api/wxxcx/trend.php',
                    filePath: i[l],
                    name: 'pic',
                    formData: {
                      submitok: 'trend_add_uppic',
                      openid: e.openid
                    },
                    success: function (i) {
                      if (1 == (i = t.jsoneval(i.data)).flag) {
                        r.push(i._s);
                        {
                          l++;
                          if (l < c) {
                            setTimeout(function () {
                              o();
                            }, 300);
                          } else {
                            uni.hideLoading();
                            var u = r.join(',');
                            app.globalData.ajax({
                              url: 'trend.php',
                              data: {
                                submitok: 'trend_add',
                                content: s,
                                piclist: u,
                                openid: e.openid
                              },
                              success: function (t) {
                                if (1 == t.flag) {
                                  uni.hideLoading(),
                                      uni.showToast({
                                        title: t.msg,
                                        mask: true,
                                        duration: 3000
                                      }),
                                      setTimeout(function () {
                                        that.goBack();
                                      }, 1000);
                                } else {
                                  uni.showToast({
                                    title: t.msg,
                                    icon: 'none',
                                    mask: true
                                  });
                                }
                              }
                            });
                          }
                        }
                      } else {
                        uni.showModal({
                          content: i.msg,
                          confirmColor: that.color,
                          confirmText: '朕知道了',
                          showCancel: false,
                          success: function (t) {
                            uni.navigateBack();
                          }
                        });
                      }
                    }
                  });
                })();
              } else {
                app.globalData.ajax({
                  url: 'trend.php',
                  data: {
                    submitok: 'trend_add',
                    content: s,
                    openid: e.openid
                  },
                  success: function (t) {
                    1 == t.flag
                        ? (uni.showToast({
                          title: t.msg,
                          mask: true,
                          duration: 3000
                        }),
                            setTimeout(function () {
                              that.goBack();
                            }, 3000))
                        : uni.showToast({
                          title: t.msg,
                          icon: 'none',
                          mask: true
                        });
                  }
                });
              }
            }
          }
        });
      }
    },
    goBack: function () {
      const t = getCurrentPages();
      const prevPage = t[t.length - 2];
      if (prevPage && typeof prevPage.$vm?.trend_topFn === 'function') {
        prevPage.$vm.trend_topFn();
      }
      uni.navigateBack({delta: 1});
    }
    //
    // goBack: function () {
    //   var t = getCurrentPages();
    //   console.log('这是获取t值', t[t.length - 2])
    //   t[t.length - 2].trend_topFn();
    //   uni.navigateBack({
    //     delta: 1
    //   });
    // }
  }
};
</script>
<style>
@import 'trend_add.css';
</style>
